package cn.shop.dys.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hp.hpl.sparta.Text;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;

import cn.shop.dys.R;
import cn.shop.dys.activity.ProductActivity;
import cn.shop.dys.bean.ProductInfo;
import cn.shop.dys.constant.CommonConstant;
import cn.shop.dys.util.TimeUtil;
import cn.shop.lib_common.CommonUtil;


/**
 * 限时抢购，适配器，
 *
 * @param <T>
 * @author zhangjun
 */
//TODO Bug：限时特价部分条目与服务器不一样，价格基本是一样的。
public class LimitBuyItemAdapter2<T> extends BaseAdapter {

    private List<T> objects;
    private Context context;
    private LayoutInflater layoutInflater;
    private int mLeftTime;
    static long startTime;

    static {
        startTime = SystemClock.uptimeMillis();
    }

    public LimitBuyItemAdapter2(Context context, List<T> datas) {
        this.context = context;

        this.layoutInflater = LayoutInflater.from(context);
        if (datas != null) {
            objects = datas;
        } else {
            objects = new ArrayList<T>();
        }
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public T getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holdedr = null;
        if (convertView == null) {
            holdedr = new ViewHolder();
        } else {
            holdedr = (ViewHolder) convertView.getTag();
        }
        holdedr.setPosition(position);
        holdedr.initializeViews();
        return holdedr.rootView;
    }

    protected class ViewHolder implements View.OnClickListener {
        private View rootView;
        private ImageView ivLimitBuyItemPic;
        private TextView tvLimitBuyItemTitle;
        private TextView tvLimitBuyItemPrice;
        private TextView tvLimitBuyItemLimitprice;
        private TextView tvLimitBuyItemRemainTime;
        private LinearLayout llLimitBuyItemFastbuy;
        private int position;
        private ProductInfo prod;
        private final TextView tv_qg_tag;
        private Handler mHandler = new Handler();


        public ViewHolder() {

            rootView = layoutInflater.inflate(R.layout.limit_buy_item, null);
            rootView.setTag(this);
            ivLimitBuyItemPic = (ImageView) rootView.findViewById(R.id.iv_limit_buy_item_pic);
            tvLimitBuyItemTitle = (TextView) rootView.findViewById(R.id.tv_limit_buy_item_title);
            tvLimitBuyItemPrice = (TextView) rootView.findViewById(R.id.tv_limit_buy_item_price);
            tvLimitBuyItemLimitprice = (TextView) rootView.findViewById(R.id.tv_limit_buy_item_limitprice);
            tvLimitBuyItemRemainTime = (TextView) rootView.findViewById(R.id.tv_limit_buy_item_remain_time);
            llLimitBuyItemFastbuy = (LinearLayout) rootView.findViewById(R.id.ll_limit_buy_item_fastbuy);
            tv_qg_tag = (TextView) rootView.findViewById(R.id.tv_qg_tag);
            tvLimitBuyItemTitle.setOnClickListener(this);

        }

        public void setPosition(int position) {
            this.position = position;
            prod = (ProductInfo) (objects.get(position));
        }

        boolean isRuning;

        private void initializeViews() {
            //TODO implement

            String picUrl = prod.getPic();
            String name = prod.getName();
            float price = prod.getPrice();
            float limitPrice = prod.getLimitPrice();
            mLeftTime = prod.getLeftTime();
            picUrl = CommonConstant.HOST + picUrl;
            Glide.with(context).load(picUrl).into(ivLimitBuyItemPic);
            tvLimitBuyItemTitle.setText(name);
            tvLimitBuyItemPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            tvLimitBuyItemPrice.setText("￥" + price);
            tvLimitBuyItemLimitprice.setText("￥" + limitPrice);
            int leftTime = prod.getLeftTime();
            leftTime -= (SystemClock.uptimeMillis() - startTime) / 1000;
            prod.setLeftTime(leftTime);
            Log.d("限时抢购适配器", "时间" + leftTime);
            if (mLeftTime > 0) {
                tvLimitBuyItemRemainTime.setText(TimeUtil.secToTime(leftTime));
                llLimitBuyItemFastbuy.setOnClickListener(ViewHolder.this);
            } else {
                tvLimitBuyItemRemainTime.setText(TimeUtil.secToTime(leftTime));
                tv_qg_tag.setText("活动结束");
                llLimitBuyItemFastbuy.setEnabled(false);//不能点击
            }
            if (!isRuning&&prod.getLeftTime()>0) {
                isRuning = true;
                CommonUtil.runOnThread(task);
            }
        }

        Runnable task = new Runnable() {
            @Override
            public void run() {
                int leftTime = prod.getLeftTime();
                while (leftTime>=0){
                    SystemClock.sleep(1000);

                leftTime -= 1;
                prod.setLeftTime(leftTime);
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (mLeftTime > 0) {
                            tvLimitBuyItemRemainTime.setText(TimeUtil.secToTime(prod.getLeftTime()));
                            llLimitBuyItemFastbuy.setOnClickListener(ViewHolder.this);

                        } else {
                            tv_qg_tag.setText("活动结束");
                            llLimitBuyItemFastbuy.setEnabled(false);//不能点击

                        }
                    }
                });}
                isRuning=false;
            }
        };

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.iv_limit_buy_item_pic:
                case R.id.ll_limit_buy_item_fastbuy:
                case R.id.tv_limit_buy_item_title:
                    Intent intent = new Intent(context, ProductActivity.class);
                    intent.putExtra(CommonConstant.PRODUCT_ID_KEY, prod.getId());
                    context.startActivity(intent);
                    break;
            }
        }
    }
}
